package ms.pc.inf;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Events extends ListActivity{

	public MyCustomBaseAdapter adapter1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final ListView lv = getListView();
	DBConnectivity objDB =  new DBConnectivity();
		objDB.getAllEvents();
		adapter1 =  new MyCustomBaseAdapter(this, DBConnectivity.arrAllEvents);
		lv.setAdapter(adapter1);
		lv.setTextFilterEnabled(true);


		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Do you want to attend this event?")
		.setCancelable(false)
		.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();

				attendEvent();
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();

				eventSelected();
			}
		});

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Object o = lv.getItemAtPosition(position);
				EventInfo selectedEvent = (EventInfo)o;
				DBConnectivity.selectedEventID = selectedEvent.getEventID();
				DBConnectivity.eventLongitude = selectedEvent.getLongitude();
				DBConnectivity.eventLatitude = selectedEvent.getLatitude();
				AlertDialog alert = builder.create();
				alert.show();
			}
		});
	}


	private void eventSelected()
	{
		System.out.println(DBConnectivity.selectedEventID);
		System.out.println(DBConnectivity.eventLatitude);
		System.out.println(DBConnectivity.eventLongitude);
		startActivity(new Intent(this, EventMapLocation.class));
	}
	private void attendEvent()
	{
		System.out.println(DBConnectivity.selectedEventID);
		System.out.println(DBConnectivity.eventLatitude);
		System.out.println(DBConnectivity.eventLongitude);
		DBConnectivity objDB =  new DBConnectivity();
		objDB.confirmAttendingEvent();
		objDB.getUsersByEvents();
		startActivity(new Intent(this, AttendingUsers.class));
	}

}
